package com.blacamdk.ChatNetModule;

import com.ck.GUI.ChatFrame;
import com.ck.GUI.LoginFrame;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * Utils
 *
 * @author BlacAmDK
 * @date 16-12-20
 */
public class Utils {

    public static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }
    public static String[] Analyze(String s)
    {
        String[] ret;
        ret = s.split(" ");
        if(!isNumeric(ret[0]))
            return null;
        return ret;
    }
    public static String getInf(String code)
    {
        String ret = "";
        switch (code)
        {
            case "OK":
                ret = "成功";
                break;
            case "WRONG_KEY":
                ret = "密钥错误";
                break;
            case "WRONG_PASSWORD":
                ret = "密码错误";
                break;
            case "USERNAME_EXIST":
                ret = "用户已存在";
                break;
            case "USERNAME_NOT_EXIST":
                ret = "用户不存在";
                break;
            case "FAILED":
                ret = "失败";
                break;
            case "FAILED_TOO_MANY":
                ret = "密码错误次数过多，请稍候重试";
                break;
            case "OTHER":
                ret = "未知错误";
                break;
            case "TIMEOUT":
                ret = "连接超时";
                break;
            default:
                ret = "其他错误";
                break;
        }
        return ret;
    }
    public static String formatTime(long time)
    {
        return new SimpleDateFormat("HH:mm:ss").format(new Date(time));
    }

    public static boolean addContent(int UID, long time, String Content, boolean me)
    {
        int index = ClientMain.getIndexFromUID(UID);
        if(index < 0)
            return false;
        User u = ClientMain.userVector.get(index);
        if(me == false)
            u.Content += u.getUsername();
        else
            u.Content += LoginFrame.username;
        u.Content += (" " + Utils.formatTime(time) + "\n" + Content + "\n");
        return true;
    }
}
